package com.sgl.gk.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.utils.ServletUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.common.utils.poi.ExcelUtil;
import com.sgl.gk.controller.vo.GkProductionLineVo;
import com.sgl.gk.controller.vo.GkSparePartsVo;
import com.sgl.gk.controller.vo.GkStationConfigurationVo;
import com.sgl.gk.controller.vo.ReworkVo;
import com.sgl.gk.domain.*;
import com.sgl.gk.mapper.*;
import com.sgl.gk.service.GkSparePartsService;
import com.sgl.gk.service.GkStationConfigurationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.sgl.common.utils.PageUtils.startPage;

@Service
@Slf4j
public class GkSparePartsServiceImpl extends BaseController implements GkSparePartsService{

    @Autowired
    GkStationConfigurationValuesMapper gkStationConfigurationValuesMapper;

    @Autowired
    GkStationMapper gkStationMapper;

    @Autowired
    GkProductionLineMapper gkProductionLineMapper;

    @Autowired
    GkStationConfigurationMapper gkStationConfigurationMapper;

    @Autowired
    GkProductMapper gkProductMapper;

    @Autowired
    GkSparePartsMapper gkSparePartsMapper;

    @Autowired
    GkStationConfigurationConnectMapper gkStationConfigurationConnectMapper;

    @Autowired
    GkDataValueLogMapper gkDataValueLogMapper;

    @Transactional
    @Override
    public AjaxResult selectByBarCode(String barCode) {
        // 根据产品下面的所有工位id找到所有配置类型为2的配置变量信息
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByValue(barCode);
        if (CollUtil.isEmpty(gkStationConfigurationValues)){
            gkStationConfigurationValues = new ArrayList<>();
            List<GkDataValueLog> gkDataValueLogList = gkDataValueLogMapper.selectAll();
            if (CollUtil.isNotEmpty(gkDataValueLogList)){
                for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                    List<GkStationConfigurationValues> gkStationConfigurationValuesList = gkStationConfigurationValuesMapper.selectByValueAndByTableName(gkDataValueLog.getSurfaceName(), barCode);
                    if (CollUtil.isNotEmpty(gkStationConfigurationValuesList)){
                        gkStationConfigurationValues.addAll(gkStationConfigurationValuesList);
                    }
                }
            }
        }
        long total = getDataTable(gkStationConfigurationValues).getTotal();
        if (gkStationConfigurationValues==null){
            return AjaxResult.success(getDataTable(new ArrayList<>()));
        }
        List<Long> stationConfigurationConnectId = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationConnectId).collect(Collectors.toList());
        Long[] longs = new Long[stationConfigurationConnectId.size()];
        for (int i = 0; i < stationConfigurationConnectId.size(); i++) {
            longs[i] = stationConfigurationConnectId.get(i);
        }
        if (longs.length==0){
            return AjaxResult.success(getDataTable(new ArrayList<>()));
        }
        List<GkStationConfigurationConnect> configurationConnect = gkStationConfigurationConnectMapper.selectInfoByIds(longs);

        List<Long> configIds = gkStationConfigurationValues.stream().map(GkStationConfigurationValues::getStationConfigurationId).collect(Collectors.toList());
        List<GkStationConfiguration> gkStationConfigurationList = gkStationConfigurationMapper.selectInfoByIds(configIds);
        List<Long> stationIds = gkStationConfigurationList.stream().map(GkStationConfiguration::getStationId).distinct().collect(Collectors.toList());
        List<GkStation> gkStations = gkStationMapper.selectInfosByIds(stationIds);
        List<GkProductionLineVo> productionLineVos = gkProductionLineMapper.list(null);
        List<ReworkVo> reworkVos = new ArrayList<>();
        for (GkStationConfigurationValues gkStationConfigurationValue : gkStationConfigurationValues) {
            ReworkVo reworkVo = new ReworkVo();
            for (GkStationConfigurationConnect gkStationConfigurationConnect : configurationConnect) {
                if (gkStationConfigurationConnect.getId().longValue()==gkStationConfigurationValue.getStationConfigurationConnectId().longValue()) {
                    reworkVo.setProductName(gkStationConfigurationConnect.getProductName());
                }
            }
            reworkVo.setConfigValuesId(gkStationConfigurationValue.getId());
            GkStationConfiguration gkStationConfiguration1 = null;
            for (GkStationConfiguration gkStationConfiguration : gkStationConfigurationList) {
                if (gkStationConfiguration.getId().longValue()==gkStationConfigurationValue.getStationConfigurationId().longValue()) {
                    gkStationConfiguration1 = gkStationConfiguration;
                    break;
                }
            }
            if (gkStationConfiguration1!=null) {
                for (GkStation gkStation : gkStations) {
                    if (gkStationConfiguration1.getStationId().longValue() == gkStation.getId().longValue()) {
                        reworkVo.setStationId(gkStation.getId());
                        for (GkProductionLineVo productionLineVo : productionLineVos) {
                            if (productionLineVo.getId().longValue()==gkStation.getProductionLineId().longValue()) {
                                reworkVo.setStationName(productionLineVo.getName()+"："+gkStation.getName());
                            }
                        }
                        if (StringUtils.isEmpty(reworkVo.getStationName())){
                            reworkVo.setStationName(gkStation.getName());
                        }
                    }
                }
            }
            reworkVo.setSparePartsCode(gkStationConfigurationValue.getValue());
            reworkVo.setCreateAt(gkStationConfigurationValue.getCreateAt());
            reworkVos.add(reworkVo);
        }

        TableDataInfo dataTable = getDataTable(reworkVos);
        dataTable.setTotal(total);
        return AjaxResult.success(dataTable);
    }

    @Transactional
    @Override
    public AjaxResult replace(GkSparePartsVo gkSparePartsVo) {
        List<GkStationConfigurationValues> gkStationConfigurationValues = gkStationConfigurationValuesMapper.selectByValue(gkSparePartsVo.getBeforePartCode());
        List<GkDataValueLog> gkDataValueLogList = gkDataValueLogMapper.selectAll();
        if (CollUtil.isEmpty(gkStationConfigurationValues)){
            gkStationConfigurationValues = new ArrayList<>();
            if (CollUtil.isNotEmpty(gkDataValueLogList)){
                for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                    List<GkStationConfigurationValues> gkStationConfigurationValuesList = gkStationConfigurationValuesMapper.selectByValueAndByTableName(gkDataValueLog.getSurfaceName(), gkSparePartsVo.getBeforePartCode());
                    if (CollUtil.isNotEmpty(gkStationConfigurationValuesList)){
                        gkStationConfigurationValues.addAll(gkStationConfigurationValuesList);
                    }
                }
            }
        }
        if (gkStationConfigurationValues==null||gkStationConfigurationValues.size()==0){
            return AjaxResult.error("未找到需更换零部件");
        }
        GkSpareParts gkSpareParts = new GkSpareParts();
        gkSpareParts.setProductId(gkSparePartsVo.getProductId());
        gkSpareParts.setProductName(gkSparePartsVo.getProductName());
        gkSpareParts.setConfigValuesId(gkSparePartsVo.getConfigValuesId());
        gkSpareParts.setBeforePartCode(gkSparePartsVo.getBeforePartCode());
        gkSpareParts.setCode(gkSparePartsVo.getCode());
        gkSpareParts.setExplain(gkSparePartsVo.getExplain());
        Date date = new Date();
        gkSpareParts.setCreateAt(date);
        gkSpareParts.setLastModifyAt(date);
        int row = gkSparePartsMapper.save(gkSpareParts);
        if (row>0){
            row = gkStationConfigurationValuesMapper.editValueById(gkSparePartsVo);
            if (row == 0){
                if (CollUtil.isNotEmpty(gkDataValueLogList)){
                    for (GkDataValueLog gkDataValueLog : gkDataValueLogList) {
                        gkStationConfigurationValuesMapper.editValueByIdAndTableName(gkDataValueLog.getSurfaceName(),gkSparePartsVo);
                    }
                }
            }
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    public List<GkSparePartsVo> listHistory(ReworkVo reworkVo) {
        List<GkSpareParts> gkSparePartsList = gkSparePartsMapper.selectInfoByBeforePartCode(reworkVo);
        List<GkSparePartsVo> gkSparePartsVos = new ArrayList<>();
        for (GkSpareParts gkSpareParts : gkSparePartsList) {
            GkSparePartsVo gkSparePartsVo1 = new GkSparePartsVo();
            gkSparePartsVo1.setId(gkSpareParts.getId());
            gkSparePartsVo1.setProductId(reworkVo.getProductId());
            gkSparePartsVo1.setProductName(reworkVo.getProductName());
            gkSparePartsVo1.setCode(gkSpareParts.getCode());
            gkSparePartsVo1.setBeforePartCode(gkSpareParts.getBeforePartCode());
            gkSparePartsVo1.setCreateAt(gkSpareParts.getCreateAt());
            gkSparePartsVos.add(gkSparePartsVo1);
        }
        return gkSparePartsVos;
    }

    @Override
    public void export(ReworkVo reworkVo, HttpServletResponse response) {
        List<GkSparePartsVo> gkSparePartsVos = listHistory(reworkVo);
        if (gkSparePartsVos==null||gkSparePartsVos.size()==0){
            return;
        }
        ExcelUtil<GkSparePartsVo> gkSparePartsVoExcelUtil = new ExcelUtil<>(GkSparePartsVo.class);
        gkSparePartsVoExcelUtil.exportExcel(response,gkSparePartsVos,"返修记录表");
    }

    @Override
    public Long countBySparePartsCode(ReworkVo reworkVo) {
        return gkSparePartsMapper.countBySparePartsCode(reworkVo);
    }
}
